package com.dingup.paike.service.pk.impl;

import com.alibaba.fastjson.JSONObject;
import com.dingup.api.model.classInfo.ClassUserInfo;
import com.dingup.paike.bean.dtobean.PkLessonInfoDTO;
import com.dingup.paike.bean.querybean.TimeInterval;
import com.dingup.paike.dao.PkLessonInfoMapper;
import com.dingup.paike.dao.PkLessonUserMapper;
import com.dingup.paike.enums.PkLessonUserTypeEnum;
import com.dingup.paike.model.pk.PkLessonInfo;
import com.dingup.paike.model.pk.PkLessonUser;
import com.dingup.paike.service.pk.PkLessonUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by wancheng on 2018/9/29.
 */
@Service
public class PkLessonUserServiceImpl implements PkLessonUserService {

    private static Logger logger = LoggerFactory.getLogger(PkLessonUserServiceImpl.class);

    @Autowired
    PkLessonUserMapper pkLessonUserMapper;
    @Autowired
    PkLessonInfoMapper pkLessonInfoMapper;

    @Override
    public void deletePkLessonStudent(ClassUserInfo classUserInfo) {
        PkLessonUserTypeEnum type = PkLessonUserTypeEnum.STUDENT;
        switch (classUserInfo.getType()) {
            case STUDENT:
                type = PkLessonUserTypeEnum.STUDENT;
                break;
            case TEACHER:
                type = PkLessonUserTypeEnum.TEACHER;
                break;
            case CLASSTEACHER:
                type = PkLessonUserTypeEnum.MASTER;
                break;
            default:
                break;
        }
        //如果是老师,不做任何处理
        if (type.equals(PkLessonUserTypeEnum.TEACHER)) {
            return;
        }
        logger.info("班级删除,执行,classUserInfo={}", JSONObject.toJSON(classUserInfo));
        pkLessonUserMapper.deleteStudentByStudentId(classUserInfo.getUserId(), classUserInfo.getClassId(), type);
    }

    @Override
    public void addPkLessonStudent(ClassUserInfo classUserInfo) {
        Date currentDate = new Date();
        List<PkLessonInfo> lessonInfos = pkLessonInfoMapper.getPkLessonByClassId(classUserInfo.getClassId(), currentDate, null);
        PkLessonUser user = new PkLessonUser();
        user.setUserId(classUserInfo.getUserId());
        user.setUserName(classUserInfo.getUserName());
        user.setClassHour(0f);
        user.setCreateTime(currentDate);
        List<PkLessonUser> saveBatch = new ArrayList<>();
        lessonInfos.forEach(
                l -> {
                    try {
                        PkLessonUser member = user.clone();
                        member.setPkLessonId(l.getId());
                        switch (classUserInfo.getType()) {
                            case CLASSTEACHER:
                                member.setType(PkLessonUserTypeEnum.MASTER);
                                saveBatch.add(member);
                                break;
                            case STUDENT:
                                member.setType(PkLessonUserTypeEnum.STUDENT);
                                saveBatch.add(member);
                                break;
                            //老师不做任何处理
                            default:
                                break;
                        }

                    } catch (CloneNotSupportedException e) {
                        e.printStackTrace();
                    }
                }
        );
        if (!CollectionUtils.isEmpty(saveBatch)) {
            logger.info("班级添加,执行,classUserInfo={}", JSONObject.toJSON(saveBatch));
            pkLessonUserMapper.insertForBatch(saveBatch);
        }
    }

    @Override
    public List<PkLessonInfoDTO> getByUserIdAndStartTimeAndAfterTime(List<String> userIds, List<TimeInterval> timeIntervals) {
        List<PkLessonInfoDTO> res = pkLessonInfoMapper.getByUserIdsAndTimeInterval(userIds, timeIntervals);

        return res;
    }
}
